import { env,findEmptyKeys } from '../../utils/index'

interface registerRes {
    data: messageData,
    code: number,
    message: string,
}
interface messageData {
    RegistrationDate: number
    code: number
    message: string
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        registerInputForm: {},

    },
    handleChangeInputPhone: function (e: any) {
        const username = e.detail.value
        this.setData({
            'registerInputForm.username': username,
        })
    },
    handleChangeInputPassword: function (e: any) {
        const passWord = e.detail.value
        this.setData({
            'registerInputForm.passWord': passWord,
        })
    },
    handleLoginRule: function () {
        const emptyKeys: string[] = findEmptyKeys(this.data.registerInputForm)
        if (emptyKeys.length) {
            wx.showModal({
                title: '校验失败',
                content: "手机号与密码均不能为空",
                showCancel: false,
            })
            return false
        }
        return true

    },
    handleToLogin: () => {
        wx.redirectTo({
            url: '/pages/login/login'
        });
    },
    handelregister: async function () {
        if (!this.handleLoginRule()) return
        wx.showLoading({ title: '注册中...' });
        wx.request({
            url: env.API_BASE_URL + '/register',
            method: 'POST',
            data: {
                ...this.data.registerInputForm
            },
            success: (res) => {
                const responseData = res.data as registerRes;
                console.log(responseData);

                if (responseData.code) {
                    wx.showToast({
                        title: '注册成功',
                        icon: 'success',
                        mask: true,
                        duration: 5000
                    })
                    wx.redirectTo({ url: '/pages/login/login' })
                    return
                }
                wx.showToast({
                    title: responseData.message,
                    icon: 'error',
                    mask: true,
                    duration: 2000
                })
            },
            fail: () => {
                wx.showToast({
                    title: '网络错误',
                    icon: 'error',
                    mask: true
                })
            },
        })
        wx.hideLoading()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})